package com.example.asus.worldcup;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;

public class TeamsActivity extends AppCompatActivity {
    ListView mLvTeams;
    ArrayList<Team>teams;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teams);
        initData();
        initView();
        initEvent();

    }

    private void initEvent() {
        mLvTeams.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Team team=teams.get(position);
                Intent intent=new Intent(TeamsActivity.this,DetailActivity.class);
                intent.putExtra("team", (Serializable) team);
                startActivity(intent);



            }
        });
    }
    //将json文件中的数据，读取，并解析
    private void initData() {
        //将json文件读取到内存字符串中
        String jsonStr = getJsonstr();
        teams =getTeams(jsonStr);
    }


    private ArrayList<Team> getTeams(String jsonStr) {
        ArrayList<Team> teamArrayList=new ArrayList<>();

        try {
            JSONArray jsonArray=new JSONArray(jsonStr);
            for (int i=0;i<jsonArray.length(); i++){
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                Team team=new Team();
                team.setId(jsonObject.getString("id"));
                team.setGroup(jsonObject.getString("group"));
                team.setName(jsonObject.getString("name"));
                team.setSheng(jsonObject.getInt("sheng"));
                team.setPing(jsonObject.getInt("ping"));
                team.setWingoal(jsonObject.getInt("wingoal"));
                team.setLosegoal(jsonObject.getInt("losegoal"));
                team.setTotalgoal(jsonObject.getInt("totalgoal"));
                team.setScore(jsonObject.getInt("score"));
                team.setPic(jsonObject.getString("pic"));
                team.setDetail(jsonObject.getString("detail"));
                teamArrayList.add(team);

            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return teamArrayList;

    }

    private String getJsonstr() {
        InputStream inputStream=getResources().openRawResource(R.raw.teams);
        byte[] bytes=new  byte[102400];
        try {
            int len=inputStream.read(bytes);
            String str =new String(bytes,0,len);
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void initView() {
        mLvTeams=findViewById(R.id.lv_teams);
        MyAdapter adapter=new MyAdapter();
        mLvTeams.setAdapter(adapter);

    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return teams.size();
        }

        @Override
        public Object getItem(int position) {
            return teams.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=View.inflate(TeamsActivity.this,R.layout.list_item_layout,null);
            TextView tvGroupId=view.findViewById(R.id.tv_group_id);
            tvGroupId.setText(teams.get(position).getId());
            ImageView ivPic=view.findViewById(R.id.iv_team);
            String pic=teams.get(position).getPic();
            TextView tvMatch = view.findViewById(R.id.tv_match);
            //"比赛情况："+"胜"+team.getSheng()+"负"+team.getFu()+"平"+team.getPing()
            tvMatch.setText("比赛情况：" + "胜" + teams.get(position).getSheng() + "负" + teams.get(position).getFu() + "平" + teams.get(position).getPing());

            TextView tvGoal = view.findViewById(R.id.tv_goal);
            tvGoal.setText("进球情况：" + "进球" + teams.get(position).getWingoal() + "失球" + teams.get(position).getLosegoal() + "净胜球" + teams.get(position).getTotalgoal());

            TextView tvScore = view.findViewById(R.id.tv_score);
            tvScore.setText("" + teams.get(position).getScore());

            int mipmap=getResources().getIdentifier(pic,"mipmap",getPackageName());
            ivPic.setImageResource(mipmap);

            return view;
        }

    }
}
